/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gruppo12;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Michele
 */
public class DbConnection {
        private static String DB_DRIVER="org.postgresql.Driver" ;
	private static String DB_CONNECTION="jdbc:postgresql://localhost:5432/gruppo12" ;
	private static String DB_USER="postgres" ;
	private static String DB_PASSWORD="gruppo12" ;
	
	static Connection connection;
        
         public static void connetti() {
      
        
        
        System.out.println("-------- PostgreSQL "
				+ "JDBC Connection Testing ------------");
   try {
 
			Class.forName(DB_DRIVER);
 
		} catch (ClassNotFoundException e) {
 
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			return;
 
		}
  System.out.println("PostgreSQL JDBC Driver Registered!");
 
		Connection connection = null;
 try {
 
			connection = DriverManager.getConnection(
					DB_CONNECTION,DB_USER,DB_PASSWORD);
 
		} catch (SQLException e) {
 
			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return;
 
		}
 
		if (connection != null) {
			System.out.println("You made it, take control your database now!");
		} else {
			System.out.println("Failed to make connection!");
		}// TODO code application logic here
    }//check della connessione al db
 //check delle primary key ritorna true se riuscito
	public boolean controllo(String value,String key,String Tabella) throws SQLException{
            Connection dbConnection = null;
		
             
 
		String selectTableSQL = "SELECT "+key
				+ " FROM "
				+ Tabella;
                
           try{ dbConnection = getDBConnection();
                 dbConnection.setAutoCommit(false);
                 java.sql.Statement statement = dbConnection.createStatement();
                java.sql.ResultSet rs = statement.executeQuery(selectTableSQL);
                while(rs.next()){if (rs.getString(1).toUpperCase().equals(value.toUpperCase())) return false;}
                return true;
              }
           catch (SQLException e) {
 
			System.out.println(e.getMessage());
			dbConnection.rollback();
                        return false;
 
		} finally {
                           if (dbConnection != null) {
				dbConnection.close();
			}
 
		}
            
        }

	  public Connection getDBConnection() {
 
		Connection dbConnection = null;
 
		try {
 
			Class.forName(DB_DRIVER);
 
		} catch (ClassNotFoundException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		try {
 
			dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
					DB_PASSWORD);
			return dbConnection;
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		return dbConnection;
 
	}
 
	private static java.sql.Timestamp getCurrentTimeStamp() {
 
		java.util.Date today = new java.util.Date();
		return new java.sql.Timestamp(today.getTime());
 
	}
	
	

}